package restaurant.agents;

import jade.core.Agent;
import jade.domain.DFService;
import jade.domain.FIPAException;
import jade.domain.FIPAAgentManagement.DFAgentDescription;
import jade.domain.FIPAAgentManagement.ServiceDescription;
import restaurant.behaviours.waiter.WaiterAvailableBehaviour;
import restaurant.behaviours.waiter.WaiterBillBehaviour;
import restaurant.behaviours.waiter.WaiterMealTradeBehaviour;
import restaurant.behaviours.waiter.WaiterServingBehaviour;
import restaurant.behaviours.waiter.WaiterSyncroBehaviour;
import restaurant.core.Restaurant;
import restaurant.gui.MainJFrame;
import restaurant.gui.Square;

public class WaiterAgent extends Agent {
	
	private static final long serialVersionUID = -3266771383381165860L;
	public boolean goalReached = false;
	
	public Restaurant restaurant;
	public int toDo = -1;
	public double score = 0;
	
	private boolean free;
	private boolean professional;
	private int experience = 0;
	private int minExperience;
	
	public restaurant.gui.Agent me;
	
	@Override
	protected void setup() {
		
		MainJFrame m = ((MainJFrame)getArguments()[0]);
		this.experience = ((Integer) getArguments()[1]);
		this.minExperience = ((Integer) getArguments()[2]);
		if(this.experience > this.minExperience)
			this.professional = true;
		else
			this.professional = false;
		
		this.free = true;
		this.restaurant = Restaurant.getInstance();
		
		DFAgentDescription agentDescription = new DFAgentDescription();
		agentDescription.setName(getAID());
		
		ServiceDescription serviceDescription = new ServiceDescription();
		serviceDescription.setType("waiters");
		serviceDescription.setName("JADE-restaurant");
		
		agentDescription.addServices(serviceDescription);
		try {
			DFService.register(this, agentDescription);
		} catch (FIPAException e) {
			e.printStackTrace();
		}
		
		this.me = new restaurant.gui.Agent(0, 1, getAID(), Square.Type.WAITER);
		m.env.agents.add(me);
		
		addBehaviour(new WaiterAvailableBehaviour(this));
		addBehaviour(new WaiterMealTradeBehaviour(this));
		addBehaviour(new WaiterServingBehaviour(this));
		addBehaviour(new WaiterBillBehaviour(this));
		addBehaviour(new WaiterSyncroBehaviour(this));
	}
	
	@Override
	protected void takeDown() {
		try {
			DFService.deregister(this);
		} catch (FIPAException e) {
			e.printStackTrace();
		}
		System.out.println("WaiterAgent: Agente " + getLocalName() + " terminato!");
	}
	
	public boolean isFree() {
		return free;
	}
	
	public void setFree(boolean free) {
		this.free = free;
	}
	
	public boolean isProfessional() {
		return professional;
	}
	
	public void setScore(double score) {
		this.score = score;
		
		this.restaurant.agentsScore.put(getAID(), this.score);
	}
	
	public double getScore() {
		return score;
	}
	
	public void setProfessional(boolean professional) {
		this.professional = professional;
	}
	
	public int getExperience() {
		return experience;
	}
	
	public void updateExperience() {
		this.experience++;
	}
	
	public int getMinExperience() {
		return minExperience;
	}
}